package com.dhkj.circle.db.sqlite;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.dhkj.circle.constant.SQLConst;
import com.dhkj.circle.constant.SystemConst;
import com.dhkj.circle.ui.AppContext;
import com.dhkj.circle.utils.DatebaseHelper;

public class InfoDBSQLite {
    private static InfoDBSQLite infoDBSQLite = new InfoDBSQLite();

    public static InfoDBSQLite getInfoDBSQLite() {
        return infoDBSQLite;
    }

    public void setSQLConst() {

        DatebaseHelper databaseHelper = new DatebaseHelper(AppContext.getInstance(), SystemConst.DATABASE_NAME, null);
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        Cursor cursor = db.query("info", new String[]{"driver_id", "dwmm", "ip_addr", "ip_port", "sql_name", "sql_pass", "sql_dbname"
        }, "", new String[]{}, null, null, null);
        //查询数据库，查看数据的记录数，看数据是否为空
        if (cursor.moveToNext()) {
//            db.execSQL(" update sysdb set sqlIpAddr=?,sqlPort=?,sqlName=?,sqlPass=?,sqlDBName=? ",
//                    new Object[]{SysConst.getSysSQLBate().getSqlIpAddr(), SysConst.getSysSQLBate().getSqlPort(),
//                            SysConst.getSysSQLBate().getSqlName(), SysConst.getSysSQLBate().getSqlPass(),
//                            SysConst.getSysSQLBate().getSqlDBName()});
            SystemConst.setDriver_id(cursor.getString(cursor.getColumnIndex("driver_id")));
            SystemConst.setDwmm(cursor.getString(cursor.getColumnIndex("dwmm")));
            SQLConst.getInstance().setSqlIpAddr(cursor.getString(cursor.getColumnIndex("ip_addr")));
            SQLConst.getInstance().setSqlPort(cursor.getString(cursor.getColumnIndex("ip_port")));
            SQLConst.getInstance().setSqlName(cursor.getString(cursor.getColumnIndex("sql_name")));
            SQLConst.getInstance().setSqlPass(cursor.getString(cursor.getColumnIndex("sql_pass")));
            SQLConst.getInstance().setSqlDBName(cursor.getString(cursor.getColumnIndex("sql_dbname")));
        } else {
            ContentValues contentValues = new ContentValues();//用来向数据库的表中插入新的行
            contentValues.put("driver_id", SystemConst.getDriver_id());
            contentValues.put("dwmm", SystemConst.getDwmm());
            contentValues.put("ip_addr", SQLConst.getInstance().getSqlIpAddr());
            contentValues.put("ip_port", SQLConst.getInstance().getSqlPass());
            contentValues.put("sql_name", SQLConst.getInstance().getSqlDBName());
            contentValues.put("sql_pass", SQLConst.getInstance().getSqlPass());
            contentValues.put("sql_dbname", SQLConst.getInstance().getSqlDBName());
            db.insert("info", null, contentValues);//插入到数据库中
        }
        cursor.close();
    }


    public void saveInfo() {
        DatebaseHelper databaseHelper = new DatebaseHelper(AppContext.getInstance(), SystemConst.DATABASE_NAME, null);
        SQLiteDatabase db = databaseHelper.getReadableDatabase();
        Cursor cursor = db.query("info", new String[]{"driver_id", "dwmm", "ip_addr", "ip_port", "sql_name", "sql_pass", "sql_dbname"
        }, "", new String[]{}, null, null, null);
        //查询数据库，查看数据的记录数，看数据是否为空
        if (cursor.moveToNext()) {
            db.execSQL(" update info set driver_id=?,dwmm=?,ip_addr=?,ip_port=?,sql_name=?,sql_pass=?,sql_dbname=?  ",
                    new Object[]{SystemConst.getDriver_id(), SystemConst.getDwmm(), SQLConst.getInstance().getSqlIpAddr(), SQLConst.getInstance().getSqlPort(),
                            SQLConst.getInstance().getSqlName(), SQLConst.getInstance().getSqlPass(),
                            SQLConst.getInstance().getSqlDBName()});

        } else {
            ContentValues contentValues = new ContentValues();//用来向数据库的表中插入新的行
            contentValues.put("driver_id", SystemConst.getDriver_id());
            contentValues.put("dwmm", SystemConst.getDwmm());
            contentValues.put("ip_addr", SQLConst.getInstance().getSqlIpAddr());
            contentValues.put("ip_port", SQLConst.getInstance().getSqlPass());
            contentValues.put("sql_name", SQLConst.getInstance().getSqlDBName());
            contentValues.put("sql_pass", SQLConst.getInstance().getSqlPass());
            contentValues.put("sql_dbname", SQLConst.getInstance().getSqlDBName());
            db.insert("info", null, contentValues);//插入到数据库中
        }
        cursor.close();
    }
}
